<?php
// +----------------------------------------------------------------------
// | 后台用户管理
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
use think\Validate;

class UserAgentReward extends Model
{
	

	
	/**
     * 错误信息
     */
	public $errorInfo;
	
	/**
     * 获取店员列表
     * @param type $shopId
     * @return type
     */
	public function getList($shopId)
    {
    	$model = new self;
		$model = $model->alias('a');
		$model = $model->leftJoin('shop_roles b','b.id = a.roles_id');
		$model = $model->where("a.shop_id",$shopId);
		$model = $model->where("a.is_del",0);
		$model = $model->field("a.id,a.name,a.img,a.phone,a.sex,a.age,a.status");
		$model = $model->field("a.roles_id,b.name as roles_name");
		$model = $model->order("a.add_time","desc");
		return $model->select();
    }
	
	/**
     * 添加店员
     * @param type $data
     * @return type
     */
	public function add($data)
    {
    	$validate = new Validate;
		$validate->rule([
			'shop_id'=>'require',
			'roles_id'=>'require',
        	'name' => 'require|length:2,5',
        	'phone' => 'require|mobile|unique:shop_staff,shop_id^phone',
		]);
		$validate->message ([
			'phone.unique'=>'该手机号或员工已存在！',
			'phone.mobile'=>'请输入正确的手机号码！',
		]);
        if( !$validate->check($data) ) {
        	$this -> errorInfo = $validate->getError();
            return false;
        }
    	self::allowField(true)  -> save($data);
		return $this;
    }
	
	/**
     * 编辑店员
     * @param type $data
     * @return type
     */
	public function edit($data)
    {
    	$validate = new Validate;
		$validate->rule([
			'id'=>'require',
			'roles_id'=>'require',
        	'name' => 'require|length:2,5',
        	'phone' => 'require|mobile|unique:shop_staff,phone,'.$data['id'].',id',
        	'status' => 'require|in:0,1',
		]);
		$validate->message ([
			'phone.unique'=>'该手机号或员工已存在！',
			'phone.mobile'=>'请输入正确的手机号码！',
		]);
        if( !$validate->check($data) ) {
        	$this -> errorInfo = $validate->getError();
            return false;
        }
    	self::allowField(true)->isUpdate(true) -> save($data);
		return $this;
    }
	
	
	/**
     * 删除代理奖励制度标准
     * @param type $id
     * @return type
     */
	public function del($id)
    {
    	return self::where('id',$id) ->delete();
    }
	
}
